package com.oms.app.user.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.oms.app.user.model.User;
import com.oms.app.user.service.IUserService;
import com.oms.base.SpringContextFactory;

/**
 * @author Loong_Mit
 * @date Sep 1, 2012 12:37:15 PM
 */

public class UserSessionIntercepter implements HandlerInterceptor{

	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object obj, Exception e)
			throws Exception {
		//每次执行需要登录权限的操作后，重新加载user
		User user = (User) request.getSession().getAttribute("user");
		if (user != null) {
			IUserService userService = (IUserService) SpringContextFactory.getBean("userService");
			User user2 = userService.getUserByRegName(user.getRegname());
			request.getSession().setAttribute("user", user2);
		}
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response,
			Object obj, ModelAndView model) throws Exception {
	}

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object obj) throws Exception {
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			response.sendRedirect("/user_login");
			return false;
		}
		
		return true;
	}

}
